#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include<locale.h>
#include<stdio.h>
#define NAVBAR_HEIGHT 7
#define CONTENT_HEIGHT 20
#define CONTENT_WIDTH 80
#define MAX_LINE_LENGTH 80
#define MAX_LINES 100
#define CONTENT_HEIGHT 20
#define CONTENT_WIDTH 80
FILE *Read_file;
char input[100] = "";
void save_Buffer_To_File();
void show_help(){}
void Edit_file()
{
	void save_Buffer_To_File();
 setlocale(LC_ALL,"");
    // 初始化ncurses模式
    initscr();
    // 不显示光标
    curs_set(0);
    // 允许特殊键值
    keypad(stdscr, TRUE);
    // 禁止行缓冲
    cbreak();
    // 禁用回显
    noecho();

    // 定义一个字符串用于存储输入的字符
    int pos = 0;

    // 主循环
    while (1) {
        // 清除屏幕
        clear();
        // 显示当前输入的字符串
        mvprintw(0, 0, "Input: %s", input);
        // 刷新屏幕以显示更改
        refresh();

        // 获取用户输入的字符
        int ch = getch();

        // 如果用户按下了退格键（ASCII值为127），则删除最后一个字符
        if (ch == 127 || ch == KEY_BACKSPACE) {
            if (pos > 0) {
                input[pos--] = '\0';
            }
        } else if (ch==27/*ch == 'q' || ch == 'Q'*/) { // 如果用户按下了ESC，退出程序
        save_Buffer_To_File();
            break;
        } else if (ch != -1 && pos < sizeof(input) - 1) { // 如果输入的是其他字符且未超过字符串长度限制
            input[pos++] = ch;
            input[pos] = '\0';
        }
    }

    // 结束ncurses模式
    endwin();
}
char *loadFile(char *filename) {
    FILE *file = fopen(filename, "r");
    if(file==NULL)
    {
    printf("无法打开文件\n");
    }
    fseek(file, 0, SEEK_END);
    long length = ftell(file);
    fseek(file, 0, SEEK_SET);

    char *contents=(char *)malloc(length+1);
    if(contents==NULL){
    printf("内存分配失败！\n");
    fclose(file);
    return NULL;
    }
        size_t read_result = fread(contents, 1, length, file);
        contents[read_result];
        fclose(file);
    return contents;
}

void openFile()
{
 char filename[20];
 char *content = NULL;
        clear();
   /*     printf("请输入文件名（按 Q 退出）：");
        scanf("%s", filename);*/
        mvprintw(1, 0, "LoadFile: (Q/q)Exit");
         initscr();
         echo();
        getstr(filename);
        if (strcmp(filename, "Q") == 0 || strcmp(filename, "q") == 0) {
            printf("退出程序。\n");
            return;
        }
     content = loadFile(filename);
     if(content==NULL){
     return ;
     }
     char *line=strtok(content,"\n");
      while(line!=NULL)
      {
      printw("%s",line);
      line=strtok(NULL,"\n");
      }
      refresh();
      char options=getch();
      if(options=='i')
      {
      Edit_file();
      }
      endwin();
      free(content);
}
void findReplace()
{

}
void fontSettings()
{
}
void draw_navbar(int selected_menu) {
clear();
   int col;
   int row;
    getmaxyx(stdscr,row,col);
    move(0, 0);
    hline(' ', col);
    mvprintw(0, 0, "Text_Edit++");
    if (selected_menu == 0) {
        attron(A_REVERSE);
        mvprintw(0, 12, "OpenFile");
        attroff(A_REVERSE);
    } else {
        mvprintw(0, 12, "OpenFile");
    }
    if (selected_menu == 1) {
        attron(A_REVERSE);
        mvprintw(0, 22, "SaveFile");
        attroff(A_REVERSE);
    } else {
        mvprintw(0, 22, "SaveFile");
    }
    if (selected_menu == 2) {
        attron(A_REVERSE);
        mvprintw(0, 32, "Eixt");
        attroff(A_REVERSE);
    } else {
        mvprintw(0, 32, "Exit");
    }
     if (selected_menu == 3) {
        attron(A_REVERSE);
        mvprintw(0, 42, "Another ");
        attroff(A_REVERSE);
    } else {
        mvprintw(0, 42, "Another");
    }
}
void save_Buffer_To_File()
{
    char fileName[20];
    printf("保存文件名：");
    scanf("%s",fileName);
    Read_file=fopen(fileName,"w");
    if (Read_file==NULL)
    {
        printf("无法保存文件\n");
        return;
    }
    fprintf(Read_file,"%s",input);
    fclose(Read_file);
    printf("写入成功！\n");
}
void Text_Edit_mian()
{
 initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    int selected_item = 0;
    int input;

    while ((input = getch())!= KEY_F(2)) {
        switch (input) {
            case KEY_UP:
                selected_item = (selected_item > 0)? selected_item - 1 : 4;
                break;
            case KEY_DOWN:
                selected_item = (selected_item < 4)? selected_item + 1 : 0;
                break;
            case KEY_ENTER:
            case '\n':
            /*
                if (selected_item == 0) {
                    mvprintw(1, 0, "OpenFile");
                    openFile();
                    clear();
                } else if (selected_item == 1) {
                    mvprintw(1, 0, "SaveFile");
                    exitCallback();
                }else if (selected_item == 2) {
        attron(A_REVERSE);
        mvprintw(0, 32, "Exit");
        attroff(A_REVERSE);
    } 
    */
     switch (selected_item) {
                    case 0:
                        openFile();
                        break;
                    case 1:
                        save_Buffer_To_File();
                        break;
                    case 2:
                        show_help();
                        break;
                    case 3:
                     printw("Exit\n");
                     endwin();
                     exit(0);
                     break;
                }
 break;
        }
        draw_navbar(selected_item);
        refresh();
    }

    endwin();
}
int main() {
   Text_Edit_mian();
}